package com.jcloud.b2c;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2017/10/16.
 */
public class ChinaCity {

    public static void main(String [] args)throws Exception{
        // String result = getUrl("http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/11/1101.html");
        // String result = getUrl("");
        Map<String,Integer> ids = new HashMap<>();
        ids.put("东城区",500);
        ids.put("西城区",501);
        ids.put("海淀区",502);
        ids.put("朝阳区",503);
        ids.put("崇文区",504);
        ids.put("宣武区",505);
        ids.put("丰台区",506);
        ids.put("石景山区",507);
        ids.put("房山区",508);
        ids.put("门头沟区",509);
        ids.put("通州区",510);
        ids.put("顺义区",511);
        ids.put("昌平区",512);
        ids.put("怀柔区",513);
        ids.put("平谷区",514);
        ids.put("大兴区",515);
        ids.put("密云区",516);
        ids.put("延庆区",517);
        // String baseUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/11/"; // 1101.html
        // String baseUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/31/"; // 3101.html
        // String baseUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/50/"; // 重庆市 5001.html
        // String baseUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/50/";  // 5002.html;
        // String baseUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/12/";  //1201.html;
        String baseUrl = "http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2016/"; // 34.html
        JSONArray json = getCity(baseUrl,"34.html");
        System.out.println(json);
    }


    private static JSONArray getQx(String baseURL,String url) throws Exception {
        String result = getUrl(baseURL + url);
        Matcher rm = Pattern.compile("<tr class='countytr'>([\\s\\S]*)</tr>").matcher(result);
        rm.find();
        String townHtml = rm.group(1);
        System.out.println(townHtml);
        Pattern p = Pattern.compile("<td>(.*?)</td>");
        Matcher m = p.matcher(townHtml);
        int i = 1;
        List<String> citys = new ArrayList<>();
        while(m.find()){
            String html = m.group();
            if(i % 2 == 0){
                citys.add(html);
                System.out.println("ssss"+html);
            }
            i ++;
        }
        JSONArray array = new JSONArray();
        for(String item : citys){
            if(item.indexOf("<a href=") <= 0){continue;}
            String [] rr = item.replaceAll("<td><a href='","")
                    .replaceAll("</a></td>","")
                    .split("'>");

            JSONObject data = new JSONObject();
            data.put("name",rr[1].replaceAll("街道","").replaceAll("办事处",""));
            array.add(data);
        }
        return array;
    }


    private static JSONArray getCity(String baseURL,String url) throws Exception{
        String result = getUrl(baseURL + url);
        System.out.println("-----------------city-----------------");
        System.out.println(result);
        System.out.println("-----------------city-----------------");
        Pattern p = Pattern.compile("<tr class='citytr'>([\\s\\S]*)</tr>");
        Matcher m = p.matcher(result);
        List<String> citys = new ArrayList<>();
        int i = 1;
        m.find();
        String res = m.group(1);
        Pattern pp = Pattern.compile("<td>(.*?)</td>");
        Matcher mm = pp.matcher(res);
        while (mm.find()){
            String sss = mm.group();
            if(i % 2 == 0 ){
                citys.add(sss);
            }
            i ++;
        }
        JSONArray array = new JSONArray();
        for(String item : citys){
            String [] rr = item.replaceAll("<td><a href='","")
                    .replaceAll("</a></td>","").split("'>");
            // System.out.println(rr[0]+":"+rr[1]);
            JSONObject data = new JSONObject();
            data.put("name",rr[1]);
            data.put("url",baseURL + rr[0]);
            array.add(data);
            JSONArray childern = getQx(baseURL,rr[0]);
            data.put("zchildren",childern);
        }
        return array;
    }

    private static String getUrl(String _url)throws  Exception{
        URL url = new URL(_url);
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        InputStream in = con.getInputStream();
        BufferedReader read = new BufferedReader(new InputStreamReader(in,"gb2312"));
        StringBuilder result = new StringBuilder();
        String temp;
        while ((temp = read.readLine()) != null){
            result.append(temp);
        }
        read.close();
        in.close();
        return result.toString();
    }

}
